/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.max.oti.system.bean.sgt;

import com.max.oti.system.bean.admin.TypeComissionBean;
import com.max.oti.system.bean.admin.TypeUserBean;
import com.max.oti.system.model.Modality;
import com.max.oti.system.service.interfac.IModalityService;
import com.max.oti.system.util.FacesUtil;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

@ManagedBean
@ViewScoped
public class ModalityBean implements Serializable{
     
     Modality modality = new Modality();
    @ManagedProperty("#{modalityService}")
    IModalityService modalityService;
    List<Modality> listModality;
    List<Modality> filterListModality;
    
    @PostConstruct
    public void initCollection()
    {
         try {
             listModality= modalityService.listHQL("FROM Modality");
         } catch (Exception ex) {
             Logger.getLogger(ModalityBean.class.getName()).log(Level.SEVERE, null, ex);
         }
    }
    
     public void saveModalityComission() {
        if (modality.getId()== null) {
            try {
                modalityService.save(modality);
                listModality.add(modality);
                modality = new Modality();
                FacesUtil.addMessage("exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                FacesUtil.addMessage("Error", null, FacesMessage.SEVERITY_FATAL);
                Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
            }

        } else {
            try {
                modalityService.update(modality);
                listModality = modalityService.listHQL("FROM Modality");
                modality = new Modality();
                FacesUtil.addMessage("exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                FacesUtil.addMessage("Error", null, FacesMessage.SEVERITY_FATAL);
                Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
     
     
    public void deleteTypeComission() {
        try {
            modalityService.delete(modality);
            listModality = modalityService.listHQL("FROM Modality");
            modality = new Modality();
        } catch (Exception ex) {
            Logger.getLogger(TypeComissionBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void onCellEdit(Object name) {
        if (name != null) {
            try {
              //  modalityService.HQLexecute("UPDATE Modality tc SET tu.name='" + name + "' WHERE tc.id=" + id);
                FacesUtil.addMessage("Modificado con Exito", null, FacesMessage.SEVERITY_INFO);
            } catch (Exception ex) {
                Logger.getLogger(TypeUserBean.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    //<editor-fold defaultstate="collapsed" desc=" METODOS GETTER Y SETTER ">
    public Modality getModality() {
        return modality;
    }

    public void setModality(Modality modality) {
        this.modality = modality;
    }
    
    
    public IModalityService getModalityService() {
        return modalityService;
    }

    public void setModalityService(IModalityService modalityService) {
        this.modalityService = modalityService;
    }

    public List<Modality> getListModality() {
        return listModality;
    }

    public void setListModality(List<Modality> listModality) {
        this.listModality = listModality;
    }

    public List<Modality> getFilterListModality() {
        return filterListModality;
    }

    public void setFilterListModality(List<Modality> filterListModality) {
        this.filterListModality = filterListModality;
    }
    
      //</editor-fold> 
}
